package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.pojo.FreightTemplatePo;
import cc.linker.steplink.crm.merchant.service.FreightTemplateService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/5/22 18:17
 * @Description: TODO
 */
@RestController
@RequestMapping("/freight")
public class FreightTemplateController {

    @Autowired
    private FreightTemplateService freightTemplateService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    //获取模板分页列表
    @GetMapping("/getFreightTemplatePageList")
    public Object getFreightTemplatePageList(@RequestParam(value = "pageIndex") Integer pageIndex, @RequestParam(value = "pageSize") Integer pageSize, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return freightTemplateService.getFreightTemplatePageList(pageIndex, pageSize, userinfo.getAdminId());
    }

    //获取模板列表
    @GetMapping("/getFreightTemplateList")
    public  Object getFreightTemplatePageList(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return freightTemplateService.getFreightTemplateList(userinfo.getAdminId());
    }

    @GetMapping("/getFreightTemplateModel")
    public FreightTemplatePo getFreightTemplateModel(@RequestParam(value = "templateId") String templateId){
        return freightTemplateService.getFreightTemplateModel(templateId);
    }

    //添加运费模板
    @PostMapping("/insertFreightTemplate")
    public void insertFreightTemplate(@RequestBody FreightTemplatePo freightTemplateInfo, HttpServletRequest request) throws UnsupportedEncodingException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        if (StringUtils.isEmpty(freightTemplateInfo.templateName)) {
            throw GlobalException.build("请输入模板名称");
        }
        if (freightTemplateInfo.freightAreaPos.size() == 0) {
            throw GlobalException.build("请添加配送区域");
        }
        //运费模板
        freightTemplateInfo.customerId = userinfo.getAdminId();
        freightTemplateInfo.orgId = userinfo.getOrgId();
        freightTemplateService.insertFreightTemplate(freightTemplateInfo);
    }

    //删除运费模板
    @PostMapping("/removeFreightTemplate")
    public void removeFreightTemplate(@RequestBody Map<String, Object> params, HttpServletRequest request) throws UnsupportedEncodingException {
        String templateId = params.containsKey("templateId") ? params.get("templateId").toString() : "";
        if (StringUtils.isEmpty(templateId)) {
            throw GlobalException.build("模板ID不能为空");
        }
        if (!freightTemplateService.removeFreightTemplate(templateId)) {
            throw GlobalException.build("删除模板失败");
        }
    }
}
